package ais.mantianx.config;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author starrysky
 * @title: MyInterceptor
 * @projectName springMvc_Parent
 * @description: 拦截器-aop具体的实现
 * @date 2021/2/619:00
 */
public class MyInterceptor implements HandlerInterceptor {
    /**
     * 返回为true就会放行到controller再到postHandkler，再到afterComplate清理
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (request.getRequestURI().contains("login")){
            return true;
        }
        if (request.getSession().getAttribute("logined")!=null){
            return true;
        }

        request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request,response);
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("============目标方法执行后");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("============清理");
    }
}
